﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using BibliotecaProjeto.passe;
using BibliotecaProjeto.tipoPasse;

namespace BibliotecaProjeto.passe
{
    public class DadosPasse : persistencia.ConexaoSqlServer
    {
        #region Atributos
        private string instSql;
        #endregion

        #region Construtores
        /// <summary>
        /// Construtor da Classe
        /// </summary>
        public DadosPasse(){}
        #endregion

        #region Métodos
        #region conexão com select
        public List<Passe> selectPasse()
        {
            List<Passe> retorno = new List<Passe>();
            try
            {
               abrirConexao();
                SqlCommand cmd = new SqlCommand("SELECT codigo,dataInicial,dataFinal,horaInicial,horaFinal FROM passe ", conBD);
                SqlDataReader DbReader = cmd.ExecuteReader();

                while (DbReader.Read())
                {
                    Passe p = new Passe();
                    p.codigo = DbReader.GetInt32(DbReader.GetOrdinal("codigo"));
                    p.dataInicial = DbReader.GetDateTime(DbReader.GetOrdinal("dataInicial"));
                    p.dataFinal = DbReader.GetDateTime(DbReader.GetOrdinal("dataFinal"));
                    p.horaInicial = DbReader.GetDateTime(DbReader.GetOrdinal("horaInicial"));
                    p.horaFinal = DbReader.GetDateTime(DbReader.GetOrdinal("horaFinal"));
                    retorno.Add(p);
                }

                DbReader.Close();
                cmd.Dispose();
                fecharConexao();;
            }
            catch (Exception ex)
            {
                throw new Exception("Erro ao conectar e selecionar " + ex.Message);
            }
            return retorno;
        }
        #endregion


        public void cadastrar(Passe passe)
        {
            try
            {
               abrirConexao();
                instSql = "insert into passe (codigo, dataInicial, dataFinal, horaInicial, horaFinal) values (" + passe.codigo + "," + passe.dataInicial + "," + passe.dataFinal + ", " + passe.horaInicial + ", " + passe.horaFinal + ");";
                fecharConexao();;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        public void remover(Passe passe)
        {
            try
            {
               abrirConexao();
                instSql = "delete from passe where codigo = " + passe.codigo + "";
                fecharConexao();;
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        #endregion
    }
}